package drone;

import java.util.ArrayList;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

/**
 *
 * @author Robert Verginien Nickel
 */
public class DBSReader {
    /**
     * 
     * @return an int with the amount of all in the Database set up Spaces
     */
    private int getNumberOfSpaces() {
        BufferedReader br = null;
        int count = 0;
        try {
            br = new BufferedReader(new FileReader("DataBaseSpaces.txt"));
            while (br.readLine() != null) {
                count++;
            }
        } catch (IOException e) {
        }
        return count;
    }

    /**
     * reads the textfile to put the data into DataBaseSpaces
     * @return 
     */
    public static ArrayList<Space> textToData() {
        BufferedReader br;
        ArrayList<Space> spaces = new ArrayList<Space>();
        DataBaseSpaces dbs = DataBaseSpaces.getInstance();
        System.out.print("... reads Textfile ...");
        try {
            String thisLine;
            br = new BufferedReader(new FileReader("DataBaseSpaces.txt"));
            String[] splitted;
            do {
                thisLine = br.readLine();
                //creates an array from the with ";" splitted line
                splitted = thisLine.split(";");
                //reads the id from the array
                int id = Integer.parseInt(splitted[0]);
                //reads the xPosition from the array
                float xPos = Float.parseFloat(splitted[1]);
                //reads the yPosition from the array
                float zPos = Float.parseFloat(splitted[2]);
                //reads all the waypoints from the array
                ArrayList<Coordinates> wayPoints = new ArrayList<Coordinates>();
                for(int j=3; j < splitted.length; j = j+2)
                {
                    wayPoints.add(new Coordinates(Float.parseFloat(splitted[j]), Float.parseFloat(splitted[j+1])));
                }
                spaces.add(new Space(new Coordinates(xPos, zPos), wayPoints, id));
            } 
            while(br.ready());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return spaces;
    }
}
